import { Fir } from "./fir";
import { Bullet } from "./bullet"
import { AudioClass } from "./audio";
import skin from "./../assets/j-10c.png"
import shootimage from "./../assets/floor-bullet.png"
import audioShoot from './../assets/shoot.mp3'
import { convertImageToBase64 } from "./unit";

let baseskin;
convertImageToBase64(skin).then((source) => {
  baseskin = source
})

let base64ShootImage;
convertImageToBase64(shootimage).then(source => {
  base64ShootImage = source
})

export class J10c extends Fir {
  constructor(canvas) {
    super();
    this.setName("红机");
    this.setImage(baseskin);

    const oriWidth = parseInt(window.getComputedStyle(canvas).width)
    const oriHeight = parseInt(window.getComputedStyle(canvas).height)
    this.oriWidth = oriWidth
    this.oriHeight = oriHeight

    const width = (oriWidth < oriHeight ? oriWidth : oriHeight / 2) / 5
    this.setWidth(width);
    this.setHeight(width * 1.2);
    this.setPosition(oriWidth / 2 - this.width / 3, oriHeight * 0.75);
    
  }
  
  bullets = [];
  shooting() {
    const bullet = new Bullet();
    bullet.setName("AK-001");
    bullet.setImage(base64ShootImage);
    const width = (this.oriWidth < this.oriHeight ? this.oriWidth : this.oriHeight / 2) / 70
    bullet.setWidth(width);
    bullet.setHeight(width * 9);
    bullet.setPosition(this.position[0] + this.width * 0.5 - width * 0.5, this.position[1]);
    bullet.ondestory(() => {
      this.removeBullet(bullet)
      bullet.audio.destory();
    })
    let count = 0;
    const handel = () => {
      bullet.position[1] = bullet.position[1] - count * 1.02;
      if (bullet.position[1] + bullet.height > 0) {
        requestAnimationFrame(handel);
      } else {
        // 溢出可视区域，移除
        this.removeBullet(bullet)
        // bullet.audio.destory();
      }
      count += this.oriHeight / 15000;
    };
    handel();
    this.bullets.push(bullet);
    bullet.audio = new AudioClass(audioShoot).volume(0.2);
  }
  removeBullet(bullet) {
    const index = this.bullets.findIndex((item) => item === bullet);
    if (index > -1) {
      this.bullets.splice(index, 1);
    }
  }

  isHit(bullet) {
    const my = {
      minx: this.position[0] + this.width * 0.333,
      maxx: this.position[0] + this.width * 0.666,
      miny: this.position[1],
      maxy: this.position[1] + this.height,
    };
    const blt = {
      minx: bullet.position[0],
      maxx: bullet.position[0] + bullet.width,
      miny: bullet.position[1],
      maxy: bullet.position[1] + bullet.height,
    };

    if (my.maxx < blt.minx) return false;
    if (my.minx > blt.maxx) return false;
    if (my.maxy < blt.miny) return false;
    if (my.miny > blt.maxy) return false;

    return true;
  }

  isFocus(bullet) {
    const my = {
      minx: this.position[0] + this.width * 0.333,
      maxx: this.position[0] + this.width * 0.666,
      miny: this.position[1],
      maxy: this.position[1] + this.height,
    };
    const blt = {
      minx: bullet.position[0],
      maxx: bullet.position[0] + bullet.width,
    };

    if (my.maxx < blt.minx) return false;
    if (my.minx > blt.maxx) return false;

    return true;
  }

  checkWarn(bullets){
    const hasWarn = bullets.find(bullet => this.isFocus(bullet))
    if (hasWarn) this.openWarnVolce()
    else this.closeWarnVolce()
  }
}
